#include <windows.h>
#include "Base64.h"
#include "IBase64.h"
#include "resource.h"

const CHAR	Base64Chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
LPVOIDLIST	lpEncodeHead = NULL, lpDecodeHead = NULL;

#ifdef _DEBUG
#define Main DllMain
#else
#pragma comment(linker, "/ENTRY:Main")
#endif // _DEBUG

BOOL WINAPI Main(HINSTANCE, DWORD dwReason, LPVOID)
{
	LPVOIDLIST i;

	if(dwReason == DLL_PROCESS_DETACH) {
		for(i = lpEncodeHead; i; i = i->lpNext) {
			LocalFree(i->lpData);

			if(i->lpPrev)
				LocalFree(i->lpPrev);

			if(!i->lpNext) {
				LocalFree(i);
				break;
			}
		}

		for(i = lpDecodeHead; i; i = i->lpNext) {
			LocalFree(i->lpData);

			if(i->lpPrev)
				LocalFree(i->lpPrev);

			if(!i->lpNext) {
				LocalFree(i);
				break;
			}
		}
	}

	return TRUE;
}

BYTE Base64Index(CHAR Base64Char, BOOL bAcceptEqual)
{
	if(Base64Char == '=')
		return BYTE(bAcceptEqual - 1); // bAcceptEqual ? 0 : 0xFF
	
	// Base64Chars[0] = 'A' ... Base64Chars[25] = 'Z'
	if(Base64Char >= 'A' && Base64Char <= 'Z')
		return Base64Char - 'A';

	// Base64Chars[26] = 'a' ... Base64Chars[51] = 'z'
	if(Base64Char >= 'a' && Base64Char <= 'z')
		return Base64Char - 'a' + 26;

	// Base64Chars[52] = '0' ... Base64Chars[61] = '9'
	if(Base64Char >= '0' && Base64Char <= '9')
		return Base64Char - '0' + 52;

	// Base64Chars[62] = '+'
	if(Base64Char == '+')
		return 62;

	// Base64Chars[63] = '/'
	if(Base64Char == '/')
		return 63;

	return 0xFF;
}

// Encode: aaaaaabb bbbbcccc ccdddddd => 00AAAAAA 00BBBBBB 00CCCCCC 00DDDDDD
DWORD Encode(DWORD dwInput)
{
	return (Base64Chars[(dwInput & 0xFC0000) >> 18])	// aaaaaa..				=> 00AAAAAA
		| (Base64Chars[(dwInput & 0x3F000) >> 12] << 8)	// ......bb bbbb....	=> 00BBBBBB
		| (Base64Chars[(dwInput & 0xFC0) >> 6] << 16)	// ....cccc cc......	=> 00CCCCCC
		| (Base64Chars[dwInput & 0x3F] << 24);			//			..dddddd	=> 00DDDDDD
}

// Decode: 00AAAAAA 00BBBBBB 00CCCCCC 00DDDDDD => aaaaaabb bbbbcccc ccdddddd
DWORD Decode(DWORD dwInput, BOOL bAcceptEqual)
{
	DWORD	dwResult;
	BYTE	index;
	
	if((index = Base64Index(CHAR(dwInput >> 24), bAcceptEqual)) == 0xFF)	
		return 0xFFFFFFFF;
	else
		dwResult = DWORD(index);			// 00AAAAAA => aaaaaa..
	
	if((index = Base64Index(CHAR(dwInput >> 16), bAcceptEqual)) == 0xFF)	
		return 0xFFFFFFFF;
	else
		dwResult |= (DWORD(index) << 6);	// 00BBBBBB => ......bb bbbb....
	
	if((index = Base64Index(CHAR(dwInput >> 8), bAcceptEqual)) == 0xFF)	
		return 0xFFFFFFFF;
	else
		dwResult |= (DWORD(index) << 12);	// 00CCCCCC => ....cccc cc......
	
	if((index = Base64Index(CHAR(dwInput), bAcceptEqual)) == 0xFF)		
		return 0xFFFFFFFF;
	else
		dwResult |= (DWORD(index) << 18);	// 00DDDDDD =>			..dddddd

	return dwResult;
}

void IBase64Encode(LPCBYTE lpInput, UINT cbInput, LPSTR lpOutput)
{
	LPVOIDLIST lp;

	for(lp = lpEncodeHead; lp && LPENCODETHREADINFO(lp->lpData)->lpInput != lpInput; lp = lp->lpNext);

	if(lp) {
		LPENCODETHREADINFO(lp->lpData)->uFinished = 0;
		LPENCODETHREADINFO(lp->lpData)->bStarted = TRUE;
	}

	for(UINT i = 0; i + 2 < cbInput; i += 3, lpOutput += 4) {
		*LPDWORD(lpOutput) = Encode((lpInput[i] << 16) | (lpInput[i + 1] << 8) | lpInput[i + 2]);
		
		if(lp)
			LPENCODETHREADINFO(lp->lpData)->uFinished += 3;
	}

	switch(cbInput % 3) {
	case 1:
		*LPDWORD(lpOutput) = Encode(lpInput[i] << 16);
		lpOutput[2] = lpOutput[3] = '=';
		
		if(lp)
			LPENCODETHREADINFO(lp->lpData)->uFinished++;

		break;

	case 2:
		*LPDWORD(lpOutput) = Encode((lpInput[i] << 16) | (lpInput[i + 1] << 8));
		lpOutput[3] = '=';
		
		if(lp)
			LPENCODETHREADINFO(lp->lpData)->uFinished += 2;
		
		break;
	}
}

BOOL IBase64Decode(LPCSTR lpInput, UINT cbInput, UINT uEqualSigns, LPBYTE lpOutput)
{
	LPVOIDLIST	lp;
	DWORD		dwResult;

	for(lp = lpDecodeHead; lp && LPDECODETHREADINFO(lp->lpData)->lpOutput != lpOutput; lp = lp->lpNext);

	if(lp){
		LPDECODETHREADINFO(lp->lpData)->uFinished = 0;
		LPDECODETHREADINFO(lp->lpData)->uTotal = cbInput;
		LPDECODETHREADINFO(lp->lpData)->bStarted = TRUE;
	}

	for(UINT i = 0; i < cbInput - 4; i += 4, lpInput += 4, lpOutput += 3) {
		if((dwResult = Decode(*LPDWORD(lpInput), FALSE)) == 0xFFFFFFFF)
			return FALSE;
		
		lpOutput[0] = BYTE(dwResult >> 16);
		lpOutput[1] = BYTE(dwResult >> 8);
		lpOutput[2] = BYTE(dwResult);

		if(lp)
			LPDECODETHREADINFO(lp->lpData)->uFinished += 4;
	}

	if((dwResult = Decode(*LPDWORD(lpInput), TRUE)) == 0xFFFFFFFF)
		return FALSE;

	lpOutput[0] = BYTE(dwResult >> 16);

	if(uEqualSigns < 2)
		lpOutput[1] = BYTE(dwResult >> 8);
	
	if(!uEqualSigns) // uEqualSigns == 0
		lpOutput[2] = BYTE(dwResult);

	if(lp)
		LPDECODETHREADINFO(lp->lpData)->uFinished += 4;
	
	return TRUE;
}

DWORD WINAPI EncodeProc(LPENCODETHREADINFO lpETI)
{
	LPVOIDLIST i;

	BASE64ENCODEFUNC lpFunc =
		lpETI->bUnicode ? BASE64ENCODEFUNC(Base64EncodeW) : BASE64ENCODEFUNC(Base64EncodeA);

	if(lpEncodeHead) {
		for(i = lpEncodeHead; i->lpNext; i = i->lpNext);
		i->lpNext = (LPVOIDLIST)LocalAlloc(LPTR, sizeof(VOIDLIST));
		i->lpNext->lpPrev = i;
		i = i->lpNext;
	} else
		i = lpEncodeHead = (LPVOIDLIST)LocalAlloc(LPTR, sizeof(VOIDLIST));
	
	i->lpData = (LPVOID)lpETI;

	lpETI->lpEndCallback(lpETI->hThread,
		lpFunc(lpETI->lpInput, lpETI->cbInput, lpETI->lpOutput, lpETI->cbOutput),
		lpETI->lpvParam);
	
	LocalFree(lpETI);
	
	if(i->lpPrev)
		i->lpPrev->lpNext = i->lpNext;
	else
		lpEncodeHead = i->lpNext;
	
	if(i->lpNext)
		i->lpNext->lpPrev = i->lpPrev;

	if(i == lpEncodeHead)
		lpEncodeHead = NULL;
	
	LocalFree(i);

	return 0;
}

DWORD WINAPI DecodeProc(LPDECODETHREADINFO lpDTI)
{
	LPVOIDLIST i;

	BASE64DECODEFUNC lpFunc =
		lpDTI->bUnicode ? BASE64DECODEFUNC(Base64DecodeW) : BASE64DECODEFUNC(Base64DecodeA);

	if(lpDecodeHead) {
		for(i = lpDecodeHead; i->lpNext; i = i->lpNext);
		i->lpNext = (LPVOIDLIST)LocalAlloc(LPTR, sizeof(VOIDLIST));
		i->lpNext->lpPrev = i;
		i = i->lpNext;
	} else
		i = lpDecodeHead = (LPVOIDLIST)LocalAlloc(LPTR, sizeof(VOIDLIST));

	i->lpData = (LPVOID)lpDTI;
	
	lpDTI->lpEndCallback(lpDTI->hThread,
		lpFunc(lpDTI->lpInput, lpDTI->lpOutput, lpDTI->cbOutput),
		lpDTI->lpvParam);

	LocalFree(lpDTI);

	if(i->lpPrev)
		i->lpPrev->lpNext = i->lpNext;
	else
		lpDecodeHead = i->lpNext;
	
	if(i->lpNext)
		i->lpNext->lpPrev = i->lpPrev;

	if(i == lpDecodeHead)
		lpDecodeHead = NULL;

	LocalFree(i);
	
	return 0;
}

UINT BASE64API CharsNeededToEncode(UINT cb)
{
	if(!cb)
		return 0;

	return (((cb + 2) / 3) << 2) + 1;
}

UINT BASE64API BytesNeededToDecode(UINT cb, UINT nEqualSigns)
{
	if(cb % 4 || !cb || nEqualSigns > 2)
		return 0;
	
	return (cb >> 2) * 3 - nEqualSigns;
}

UINT BASE64API CountEqualSignsA(LPCSTR lpInput)
{
	int len;
	UINT uEqualSigns = 0;

	if(IsBadStringPtrA(lpInput, -1))
		return 0xFFFFFFFF;

	len = lstrlenA(lpInput);
	
	for(int i = len; i > 0; i--) {
		if(lpInput[i - 1] == '=')
			uEqualSigns++;
		else
			break;
		
		if(uEqualSigns > 2)
			return 0xFFFFFFFF;
	}
	
	return uEqualSigns;
}

UINT BASE64API CountEqualSignsW(LPCWSTR lpInput)
{
	int len;
	UINT uEqualSigns = 0;

	if(IsBadStringPtrW(lpInput, -1))
		return 0xFFFFFFFF;

	len = lstrlenW(lpInput);
	
	for(int i = len; i > 0; i--) {
		if(lpInput[i - 1] == L'=')
			uEqualSigns++;
		else
			break;
		
		if(uEqualSigns > 2)
			return 0xFFFFFFFF;
	}
	
	return uEqualSigns;
}

BOOL BASE64API IsValidBase64StringA(LPCSTR lpString, LPUINT lpEqualSigns)
{
	UINT	uEqualSigns = 0;
	int		i, len;
	CHAR	ch;
	BOOL	bFound;
	
	if(IsBadStringPtrA(lpString, -1) || ((len = lstrlenA(lpString)) % 4)
	|| (lpString[len - 2] == '=' && lpString[len - 1] != '='))
		return FALSE;

	for(i = 0; i < len; i++) {
		bFound = FALSE;
		ch = lpString[i];
		
		if((i == len - 1 || i == len - 2) && ch == '=') {
			uEqualSigns++;
			bFound = TRUE;
		} else
			bFound = (Base64Index(ch, FALSE) != 0xFF);
		
		if(!bFound)
			return FALSE;
	}
	
	if(!IsBadWritePtr(lpEqualSigns, sizeof(UINT)))
		*lpEqualSigns = uEqualSigns;

	return TRUE;
}

BOOL BASE64API IsValidBase64StringW(LPCWSTR lpString, LPUINT lpEqualSigns)
{
	LPSTR	lpAnsiString;
	int		len;
	BOOL	bReturnValue;
	
	if(IsBadStringPtrW(lpString, -1) || ((len = lstrlenW(lpString)) % 4))
		return FALSE;
	
	lpAnsiString = (LPSTR)LocalAlloc(LPTR, ++len);
	WideCharToMultiByte(CP_ACP, NULL, lpString, -1, lpAnsiString, len, NULL, NULL);
	bReturnValue = IsValidBase64StringA(lpAnsiString, lpEqualSigns);
	LocalFree(lpAnsiString);
	
	return bReturnValue;
}

BOOL BASE64API Base64EncodeA(LPCBYTE lpInput, UINT cbInput, LPSTR lpOutput, UINT cbOutput)
{
	if(IsBadReadPtr(lpInput, cbInput) || IsBadWritePtr(lpOutput, cbOutput)
	|| cbOutput < CharsNeededToEncode(cbInput))
		return FALSE;

	IBase64Encode(lpInput, cbInput, lpOutput);
	lpOutput[cbOutput - 1] = 0;
	return TRUE;
}

BOOL BASE64API Base64EncodeW(LPCBYTE lpInput, UINT cbInput, LPWSTR lpOutput, UINT cbOutput)
{
	LPSTR	lpOutputAnsi;
	UINT	uCharsNeeded;
	BOOL	bSuccess;
	
	if(IsBadReadPtr(lpInput, cbInput) || IsBadWritePtr(lpOutput, cbOutput)
	|| cbOutput < (uCharsNeeded = (CharsNeededToEncode(cbInput) << 1)))
		return FALSE;

	lpOutputAnsi = (LPSTR)LocalAlloc(LPTR, uCharsNeeded);
	IBase64Encode(lpInput, cbInput, lpOutputAnsi);
	bSuccess = (MultiByteToWideChar(CP_ACP, NULL, lpOutputAnsi, -1, lpOutput, cbOutput) != 0);
	LocalFree(lpOutputAnsi);
	
	return bSuccess;
}

BOOL BASE64API Base64DecodeA(LPCSTR lpInput, LPBYTE lpOutput, UINT cbOutput)
{
	UINT uLength, uEqualSigns;
	
	if(IsBadStringPtrA(lpInput, -1) || IsBadWritePtr(lpOutput, cbOutput)
	|| (uLength = lstrlenA(lpInput)) % 4 || (uEqualSigns = CountEqualSigns(lpInput)) > 2
	|| cbOutput < BytesNeededToDecode(uLength, uEqualSigns))
		return FALSE;

	return IBase64Decode(lpInput, uLength, uEqualSigns, lpOutput);
}

BOOL BASE64API Base64DecodeW(LPCWSTR lpInput, LPBYTE lpOutput, UINT cbOutput)
{
	UINT	uLength;
	LPSTR	lpAnsiInput;
	BOOL	bSuccess;
	
	if(IsBadStringPtrW(lpInput, -1) || (uLength = lstrlenW(lpInput)) % 4)
		return FALSE;

	lpAnsiInput = (LPSTR)LocalAlloc(LPTR, ++uLength);
	WideCharToMultiByte(CP_ACP, NULL, lpInput, -1, lpAnsiInput, uLength, NULL, NULL);
	bSuccess = Base64DecodeA(lpAnsiInput, lpOutput, cbOutput);
	LocalFree(lpAnsiInput);
	
	return bSuccess;
}

HANDLE BASE64API Base64EncodeAsyncA(LPCBYTE lpInput, UINT cbInput, LPSTR lpOutput, UINT cbOutput,
									BASE64CALLBACK lpEndCallback, LPVOID lpvParam)
{
	DWORD				dwThreadID;
	LPENCODETHREADINFO	eti;

	if(IsBadCodePtr((FARPROC)lpEndCallback))
		return INVALID_HANDLE_VALUE;

	eti = (LPENCODETHREADINFO)LocalAlloc(LPTR, sizeof(ENCODETHREADINFO));

	eti->bUnicode = FALSE;
	eti->bStarted = FALSE;
	eti->lpInput = lpInput;
	eti->cbInput = cbInput;
	eti->lpOutput = (LPVOID)lpOutput;
	eti->cbOutput = cbOutput;
	eti->uFinished = 0;
	eti->lpEndCallback = lpEndCallback;
	eti->lpvParam = lpvParam;
	
	return eti->hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)EncodeProc,
		eti, NULL, &dwThreadID);
}

HANDLE BASE64API Base64EncodeAsyncW(LPCBYTE lpInput, UINT cbInput, LPWSTR lpOutput, UINT cbOutput,
									BASE64CALLBACK lpEndCallback, LPVOID lpvParam)
{
	DWORD				dwThreadID;
	LPENCODETHREADINFO	eti;
	
	if(IsBadCodePtr((FARPROC)lpEndCallback))
		return INVALID_HANDLE_VALUE;
	
	eti = (LPENCODETHREADINFO)LocalAlloc(LPTR, sizeof(ENCODETHREADINFO));

	eti->bUnicode = TRUE;
	eti->bStarted = FALSE;
	eti->lpInput = lpInput;
	eti->cbInput = cbInput;
	eti->lpOutput = (LPVOID)lpOutput;
	eti->cbOutput = cbOutput;
	eti->uFinished = 0;
	eti->lpEndCallback = lpEndCallback;
	eti->lpvParam = lpvParam;
	
	return eti->hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)EncodeProc,
		eti, NULL, &dwThreadID);
}

HANDLE BASE64API Base64DecodeAsyncA(LPCSTR lpInput, LPBYTE lpOutput, UINT cbOutput,
									BASE64CALLBACK lpEndCallback, LPVOID lpvParam)
{
	DWORD				dwThreadID;
	LPDECODETHREADINFO	dti;
	
	if(IsBadCodePtr((FARPROC)lpEndCallback))
		return INVALID_HANDLE_VALUE;
	
	dti = (LPDECODETHREADINFO)LocalAlloc(LPTR, sizeof(DECODETHREADINFO));
	
	dti->bUnicode = FALSE;
	dti->bStarted = FALSE;
	dti->lpInput = (LPVOID)lpInput;
	dti->lpOutput = lpOutput;
	dti->cbOutput = cbOutput;
	dti->uFinished = 0;
	dti->uTotal = 0;
	dti->lpEndCallback = lpEndCallback;
	dti->lpvParam = lpvParam;

	return dti->hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)DecodeProc,
		dti, NULL, &dwThreadID);
}

HANDLE BASE64API Base64DecodeAsyncW(LPCWSTR lpInput, LPBYTE lpOutput, UINT cbOutput,
									BASE64CALLBACK lpEndCallback, LPVOID lpvParam)
{
	DWORD				dwThreadID;
	LPDECODETHREADINFO	dti;

	if(IsBadCodePtr((FARPROC)lpEndCallback))
		return INVALID_HANDLE_VALUE;
	
	dti = (LPDECODETHREADINFO)LocalAlloc(LPTR, sizeof(DECODETHREADINFO));
	
	dti->bUnicode = TRUE;
	dti->bStarted = FALSE;
	dti->lpInput = (LPVOID)lpInput;
	dti->lpOutput = lpOutput;
	dti->cbOutput = cbOutput;
	dti->uFinished = 0;
	dti->uTotal = 0;
	dti->lpEndCallback = lpEndCallback;
	dti->lpvParam = lpvParam;

	return dti->hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)DecodeProc,
		dti, NULL, &dwThreadID);
}

BOOL BASE64API GetEncodeAsyncState(HANDLE hThread, LPUINT uFinished, LPUINT uTotal)
{
	LPVOIDLIST i;

	if(IsBadWritePtr(uFinished, sizeof(UINT)) || IsBadWritePtr(uTotal, sizeof(UINT)))
		return FALSE;

	for(i = lpEncodeHead; i && LPENCODETHREADINFO(i->lpData)->hThread != hThread; i = i->lpNext);

	if(!i || !LPENCODETHREADINFO(i->lpData)->bStarted)
		return FALSE;

	*uFinished = LPENCODETHREADINFO(i->lpData)->uFinished;
	*uTotal = LPENCODETHREADINFO(i->lpData)->cbInput;
	return TRUE;
}

BOOL BASE64API GetDecodeAsyncState(HANDLE hThread, LPUINT uFinished, LPUINT uTotal)
{
	LPVOIDLIST i;
	
	if(IsBadWritePtr(uFinished, sizeof(UINT)) || IsBadWritePtr(uTotal, sizeof(UINT)))
		return FALSE;
	
	for(i = lpDecodeHead; i && LPDECODETHREADINFO(i->lpData)->hThread != hThread; i = i->lpNext);
	
	if(!i || !LPDECODETHREADINFO(i->lpData)->bStarted)
		return FALSE;
	
	*uFinished = LPDECODETHREADINFO(i->lpData)->uFinished;
	*uTotal = LPDECODETHREADINFO(i->lpData)->uTotal;
	return TRUE;
}